var express = require('express');
var router = express.Router();
const { uploadFile } = require("../utils/upload.js");

const { add, update, remove, get, upstate,getone ,getWait,link,adminpos} = require('../service/outletL.js')
//增加门店接口
router.post('/add', async function (req, res, next) {
    const params = req.body;
    const data = await add(JSON.parse(params.data));
    res.send(data);
});
//获取一家门店信息
router.post('/getone', async function (req, res, next) {
    const {_id} = req.body;
    const data = await getone({_id});
    res.send(data);
});
//图片上传接口;返回图片路径
router.post('/upload', async function (req, res, next) {
    const { data, success } = await uploadFile(req, {
        fileType: "outletL",//文件包含目录
        path: "./public/imgs"//图片存放路径，自定义
    });
    if (success) {
        const { url } = data;
        res.send(url);
    }
});
//门店信息修改接口
router.post('/update', async function (req, res, next) {
    const { _id, name, licenseNumber, licenseImg, address, x, y, city, corporation, phone, headImg, feature, vip, commission, state, clerks } =JSON.parse(req.body.data);
    let up = { name, licenseNumber, licenseImg, address, x, y, city, corporation, phone, headImg, feature, vip, commission, state, clerks }
    const data = await update({ _id }, up);
    res.send(data);
});
//删除接口
router.post('/del', async function (req, res, next) {
    const _id = req.body;
    const data = await remove({ _id });
    res.send(data);
});
//获取所有门店信息
router.get('/get', async function (req, res, next) {
    if (req.query.type && req.query.value) {
        let { type, value, curPage, eachPage } = req.query;
        const data = await get({ type, value, curPage, eachPage });
        res.send(data);
    } else {
        let { curPage, eachPage } = req.query;
        const data = await get({ curPage, eachPage });
        res.send(data);

    }
});
//搜索
// router.post('/search', async function (req, res, next) {
//     let { type, value, curPage, eachPage } = req.body;
//     const data = await search(type, value, curPage, eachPage);
//     res.send(data);
// });
//该状态
router.post('/upstate', async function (req, res, next) {
    let { _id, state } = req.body;
    const data = await upstate({ _id }, { state });
    res.send(data);
});
//待审核门店获取
router.post('/wait', async function (req, res, next) {
    let {state}=req.body;
    const data = await getWait(state);
    res.send(data);
});
//将新建门店ID关联到门店管理员账号
router.post('/link', async function (req, res, next) {
    let {_id,adminName}=req.body;
    const data = await link({_id,adminName});
    res.send(data);
});
//获取该账号关联的门店
router.post('/adminpos', async function (req, res, next) {
    let {adminName}=req.body;
    const data = await adminpos(adminName);
    res.send(data);
});
router.get('/posSale', async function (req, res, next) {
    let dataService=[40,50,70,80,150];
    let dataGoods=[20,35,70,200,250];
    res.send({dataService,dataGoods});
});
router.get('/posroom', async function (req, res, next) {
    let dataService=[6500,8000,3000,10000,7700];
    let dataGoods=[8800,9000,11000,15000,16600];
    res.send({dataService,dataGoods});
});

module.exports = router;
